const fs =require("fs");
const path = require("path");
const user = require("../sql/user");
const sql = require("../sql/index")

module.exports = function (){
    return(req,res,next)=>{
        const obj = req.body;
        console.log(req.body); 
        // obj.tel -= 0;
        // obj.hobby = obj.hobby.split(/,|\uff0c/);

        const oldName = path.join(__dirname,"..",req.files[0].path);
        // console.log(req.files[0].path);
        const newName = oldName + "-"+req.files[0].originalname;
        fs.renameSync(oldName, newName);

        const serverUrl = "http://localhost:65535" + newName.split("upload")[1];
        obj.avatar = serverUrl;
      
        sql.set(user,{userid:req.body.userid},obj,()=>{
            res.send(JSON.stringify({
                code:1,
                msg:"修改成功"
            }))
        })
    }
}